﻿using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace 串口助手
{
    public partial class RealTimeCurve : Form
    {
        private static readonly string StartText = "启动";
        private static readonly string StopText = "停止";
        private static readonly Color StartColor = Color.LimeGreen;
        private static readonly Color StopColor = Color.Tomato;

        private bool rtCurve_Enable = false;
        private RealTimeCurveEditor EditorForm = new RealTimeCurveEditor();
        private RealTimeCurve_Channel rtCurve_Channel = new RealTimeCurve_Channel();

        public RealTimeCurve()
        {
            InitializeComponent();

            var rtCurveChannel = rtCurve_Channel.Create("sin(x)", new byte[] { 0x12, 0x25 });
            EditorForm.rtCurveChannels.Add(rtCurveChannel);
            chart1.Series.Add(rtCurveChannel.series);

            EditorForm.rtCurveChannelsNameReg("sin(x)");
            EditorForm.rtCurveChannelDelete = rtCurveChannelDelete;
            EditorForm.FormClosing += EditorForm_FormClosing;

            // 启用双缓冲以减少闪烁
            this.DoubleBuffered = true;
        }

        public void rtCurveChannelDelete(Series series)
        {
            chart1.Series.Remove(series);
        }

        public void RT_Curve_WriteData(int channel, double yValue)
        {
            if (!rtCurve_Enable || channel >= EditorForm.rtCurveChannels.Count)
                return;

            rtCurve_Channel.SeriesWriteData(EditorForm.rtCurveChannels[channel], yValue);
        }

        public void RT_Curve_WriteData(byte[] data, int length)
        {
            if (!rtCurve_Enable)
                return;

            foreach (var ch in EditorForm.rtCurveChannels)
            {
                if (rtCurve_Channel.SeriesWriteData(ch, data, length))
                    return;
            }
        }

        public void RT_Curve_Off()
        {
            if (!rtCurve_Enable)
                return;

            rtCurve_Channel.SeriesThreadRelease();
            if (!EditorForm.IsDisposed)
                EditorForm.Close();
        }

        private void toolStripButton_RT_Curve_Cmd_Click(object sender, EventArgs e)
        {
            if (toolStripButton_RT_Curve_Cmd.Text == StartText)
            {
                StartCurve();
            }
            else
            {
                StopCurve();
            }
        }

        private void StartCurve()
        {
            toolStripButton_RT_Curve_Cmd.BackColor = StopColor;
            toolStripButton_RT_Curve_Cmd.Image = Properties.Resources.关闭;
            toolStripButton_RT_Curve_Cmd.Text = StopText;

            foreach (var ch in EditorForm.rtCurveChannels)
            {
                rtCurve_Channel.SeriesClear(ch);
            }

            rtCurve_Channel.SeriesThreadCreate(EditorForm.rtCurveChannels[0]);
            rtCurve_Enable = true;
        }

        private void StopCurve()
        {
            toolStripButton_RT_Curve_Cmd.BackColor = StartColor;
            toolStripButton_RT_Curve_Cmd.Image = Properties.Resources.开启;
            toolStripButton_RT_Curve_Cmd.Text = StartText;

            rtCurve_Channel.SeriesThreadRelease();
            rtCurve_Enable = false;
        }

        private void toolStripButton_新建_Click(object sender, EventArgs e)
        {
            contextMenuStrip_创建.Show(Control.MousePosition);
        }

        private void EditorForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            EditorForm.MsgDecodeCfg();
            EditorForm.Hide();
            e.Cancel = true;
        }

        private void toolStripButton_设置_Click(object sender, EventArgs e)
        {
            Point point = Control.MousePosition;
            point.X -= EditorForm.Width;
            EditorForm.Location = point;
            EditorForm.Show();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            string name = toolStripTextBox_CurveName.Text;
            if (string.IsNullOrEmpty(name) || EditorForm.rtCurveChannelCheck(name))
                return;

            var rtCurveChannel = rtCurve_Channel.Create(name, null);
            EditorForm.rtCurveChannels.Add(rtCurveChannel);
            chart1.Series.Add(rtCurveChannel.series);
            EditorForm.rtCurveChannelsNameReg(name);
            toolStripButton_设置_Click(null, null);
        }
    }
}
